const {
	reqGet,
	reqPost,
} = require('@utils/request')
const {
	$U_UserInfo,
} = require('@utils/storage')

module.exports = {

	/**
	 * 双击添加参数值
	 * @param  {[type]} row [description]
	 * @return {[type]}     [description]
	 */
	onDblClickRow(row) {
		let {
			store,
			requestParameterComp,
		} = this
		if (store.currentRequestParameter && store.currentRequestParameter._treeId_) {
			requestParameterComp.treegrid('endEdit', store.currentRequestParameter._treeId_)
		}
		store.currentRequestParameter = row
		requestParameterComp.treegrid('beginEdit', row._treeId_)
	},

	/**
	 * 测试接口
	 * @return {[type]} [description]
	 */
	requestTestMethod() {
		let {
			store,
			testServerUrl,
			requestParameterComp,
			responseParameterComp,
		} = this
		if (store.currentRequestParameter && store.currentRequestParameter._treeId_) {
			requestParameterComp.treegrid('endEdit', store.currentRequestParameter._treeId_)
		}
		let currentAPI = store.currentAPI
		if (!currentAPI || !currentAPI.attributes)
			return false
		let {
			method,
			path,
			contentType,
			responseParameterList,
		} = currentAPI.attributes
		let serverUrl = testServerUrl.textbox('getValue').trim()
		let requestData = requestParameterComp.treegrid('getData')
		let requestParams = {}
		for (let i = 0; i < requestData.length; i++) {
			let data = requestData[i]
			requestParams[data.name] = data.value || ''
		}
		console.log('requestParams', requestParams)
		let params = {
			serverUrl,
			method,
			path,
			contentType,
			req_param_str: JSON.stringify(requestParams)
		}
		reqPost('/ult/api/test', params).then(result => {
			console.log('result', result)
			if (!result || result.error) {
				return false
			}
			store.tmpResult = result || {}
			store.tmpResponseParameterMap = {}
			try {
				responseParameterList = JSON.parse(responseParameterList)
			} catch (e) {
				responseParameterList = []
			}
			responseParameterComp.treegrid('loadData', responseParameterList)
		})
	},
}